﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using System.Data.Linq;
using System.Linq.Expressions;
namespace BAL
{
    public class StudentScore : BaseBAL<StudentScoreInfo, Models.StudentScore>
    {
        private static string CacheName = "AllStudentScroe";

        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <returns></returns>
        public static void ToCache()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                CacheHelper.Refurbish(CacheName, db.StudentScores.ToList(), 0, 60);
            }
        }

        /// <summary>
        /// 取得一条
        /// </summary>
        /// <param name="InputId">信息ID</param>
        /// <returns></returns>
        public static StudentScoreInfo GetSingleInfo(int InputId)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.StudentScore> studentScoreInfo = (IList<Models.StudentScore>)CacheHelper.GetCache(CacheName);
            return TransT2ToT1(studentScoreInfo.Single(b => b.Id == InputId));
        }

        /// <summary>
        /// 取得全部列表
        /// </summary>
        /// <returns></returns>
        public static IList<StudentScoreInfo> GetList()
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.StudentScore> studentScoreInfo = (IList<Models.StudentScore>)CacheHelper.GetCache(CacheName);
            return studentScoreInfo.Select(t => TransT2ToT1(t)).ToList();
        }
        
        public static IList<int> GetStudentScoreIdByStudentId(int sid)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.StudentScore> studentScoreInfo = (IList<Models.StudentScore>)CacheHelper.GetCache(CacheName);
            return studentScoreInfo.Where(t => t.StudentId == sid).Select(t => t.Id).ToList();
        }

        public static IList<int> GetStudentScoreIdByExamRoomId(int eid)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.StudentScore> studentScoreInfo = (IList<Models.StudentScore>)CacheHelper.GetCache(CacheName);
            return studentScoreInfo.Where(t => t.ExamRoomId == eid).Select(t => t.Id).ToList();
        }
        
        public static IList<int> GetExamRoomIdByStudentId(int sid)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                var query = from t in db.StudentScores
                            where t.StudentId == sid
                            group t by t.ExamRoomId into g
                            select new { 
                                ExamRoomId = g.Key
                            };
                IList<int> list = new List<int>();
                foreach (var id in query)
                {
                    list.Add(id.ExamRoomId);
                }
                return list;
            }
        }
        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="InstInfo">如果id=0则新增>0时编辑</param>
        /// <returns></returns>
        public static StudentScoreInfo Save(StudentScoreInfo InstInfo)
        {
            StudentScoreInfo studentScoreInfo = InstInfo;
            if (InstInfo.Id > 0)
            {
                Edit(InstInfo);
            }
            else
            {
                studentScoreInfo = Add(InstInfo);
            }
            ToCache();
            return studentScoreInfo;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">id</param>
        public static void DelInfo(int id)
        {
            Delete(id);
            ToCache();
        }
        public static void DelInfo(IList<int> ids)
        {
            foreach (int id in ids)
            {
                DelInfo(id);
            }
        }
        public static void DelByStudentId(int id)
        {
            IList<int> ids = GetStudentScoreIdByStudentId(id);
            DelInfo(ids);
        }
        public static void DelByExamRoomId(int id)
        {
            IList<int> ids = GetStudentScoreIdByExamRoomId(id);
            DelInfo(ids);
        }
    }
}
